package com.lubase.wfengine.service.provider;

import com.lubase.core.service.RenderTableService;
import com.lubase.core.util.ClientMacro;
import com.lubase.model.DbCollection;
import com.lubase.model.DbEntity;
import com.lubase.orm.QueryOption;
import com.lubase.orm.TableFilter;
import com.lubase.orm.service.AppHolderService;
import com.lubase.orm.service.DataAccess;
import com.lubase.orm.util.TableFilterWrapper;
import com.lubase.orm.util.TypeConverterUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import com.lubase.lubaseapi.WorkFlowRendPageService;

import java.util.List;

@Slf4j
@DubboService(version = "1.0.0")
public class WfRendPageServiceImpl implements WorkFlowRendPageService {
    @Autowired
    RenderTableService renderTableService;

    @Autowired
    AppHolderService appHolderService;

    @Autowired
    DataAccess dataAccess;

    @Override
    public DbCollection getWfPageMainData(String pageId, String clientMacroStr, String searchParamStr, String queryParamsStr) {
        if (StringUtils.isEmpty(pageId) || StringUtils.isEmpty(clientMacroStr)) {
            return null;
        }
        ClientMacro clientMacro = ClientMacro.init(clientMacroStr);
        // 日志打印参数
        log.debug("getWfPageMainData:{}|{}|{}|{}", pageId, clientMacro, searchParamStr, queryParamsStr);
        try {
            DbCollection coll = renderTableService.getGridDataByPageId(pageId, clientMacro, searchParamStr, queryParamsStr, null);
            return coll;
        } catch (Exception e) {
            log.error("getWfPageMainData error:{}|", e.getMessage());
        }
        return null;
    }

    @Override
    public String getRefPageSettingByTaskId(String taskId) {
        if (StringUtils.isEmpty(taskId)) {
            log.error("getRefPageSettingByTaskId error:taskId is empty");
            return null;
        }
        QueryOption queryOption = new QueryOption("wf_task");
        queryOption.setTableFilter(new TableFilter("id", taskId));
        queryOption.setFixField("ref_page_setting");
        DbCollection coll = dataAccess.query(queryOption);
        if(coll.getData().isEmpty()){
            return null;
        }
        return TypeConverterUtils.object2String(coll.getData().get(0).get("ref_page_setting"));
    }

    @Override
    public List<DbEntity> getWfFins(String serviceId, String dataId) {
        if (StringUtils.isEmpty(serviceId) || StringUtils.isEmpty(dataId)) {
            log.error("getWfFins error:serviceId or dataId is empty");
            return null;
        }
        QueryOption queryOption = new QueryOption("wf_fins");
        TableFilterWrapper filterWrapper = TableFilterWrapper.and();
        filterWrapper.eq("service_id", serviceId).eq("data_id", dataId);
        queryOption.setTableFilter(filterWrapper.build());
        queryOption.setFixField("id");
        return dataAccess.queryAllData(queryOption).getData();
    }

    @Override
    public DbEntity getWFOInsByDataOInsId(String oInsId) {
        if (StringUtils.isEmpty(oInsId)) {
            log.error("getWFOInsByDataOInsId error:oInsId is null");
            return null;
        }
        DbCollection coll = dataAccess.queryById("wf_oins", Long.parseLong(oInsId));
        return !coll.getData().isEmpty() ? coll.getData().get(0) : null;
    }

    @Override
    public List<DbEntity> getUserProcessedOInsIdByFinsId(Long fInsId) {
        if (fInsId == null) {
            log.error("getUserProcessedOInsIdByFinsId error:fInsId is null");
            return null;
        }
        QueryOption queryOption = new QueryOption("wf_oins");
        TableFilterWrapper filterWrapper = TableFilterWrapper.and();
        filterWrapper.eq("fins_id", fInsId).eq("user_id", appHolderService.getUser().getId()).eq("process_status", "1");
        queryOption.setTableFilter(filterWrapper.build());
        queryOption.setFixField("id");
        DbCollection coll = dataAccess.query(queryOption);
        return coll.getData();
    }

    @Override
    public List<DbEntity> getUserUnProcessedOInsList(String serviceId, String dataId) {
        if (StringUtils.isEmpty(serviceId) || StringUtils.isEmpty(dataId)) {
            log.error("getUserUnProcessedOInsList error:serviceId or dataId is empty");
            return null;
        }

        String userId = appHolderService.getUser().getId().toString();
        return dataAccess.procGetDbEntityList("wf_app", "proc_getWfOInsByDataId", userId, serviceId, dataId);
    }

    @Override
    public List<String> getUserProcessDataIdList(String serviceId, String processStatus) {
        if (StringUtils.isEmpty(serviceId) || StringUtils.isEmpty(processStatus)) {
            log.error("getUserProcessDataIdList error:serviceId or processStatus is empty");
            return null;
        }
        String userId = appHolderService.getUser().getId().toString();
        return dataAccess.procGetStringList("wf_app", "proc_getUserProcessIds", userId, serviceId, processStatus);
    }
}
